package com.example.enginsdk;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;
import android.util.Log;

import com.csizg.imemodule.entity.HwrAssociateWordsResult;
import com.csizg.imemodule.entity.HwrRecogResult;
import com.csizg.imemodule.entity.HwrRecogResultItem;
import com.wwengine.hw.WWHandWrite;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * User:Gaolei  gurecn@gmail.com
 * Date:2018/4/2
 * I'm glad to share my knowledge with you all.
 */
public class HdwManager {
    private static String TAG = HdwManager.class.getSimpleName();

    private static boolean sInitState = false;

    private static boolean sIsInit = false;

    private static HdwManager manager;

    private static char[] mResult1;

    private List<Short> mSBPoint = new LinkedList<>();

    private HdwManager() {
        if (sIsInit) {
            return;
        }
        sIsInit = true;
        sInitState = false;
        sInitState = initHdw(App.getAppContext());
        sIsInit = false;
    }

    public synchronized static HdwManager getInstance() {
        if (manager == null) {
            manager = new HdwManager();
        }
        return manager;
    }

    private static byte[] readData(AssetManager am, String name) {
        try {
            InputStream is = am.open(name);
            if (is == null) {
                return null;
            }
            int length = is.available();
            if (length <= 0) {
                return null;
            }
            byte[] buf = new byte[length];
            if (is.read(buf, 0, length) == -1) {
                return null;
            }
            is.close();
            return buf;
        } catch (Exception ex) {
            return null;
        }
    }

    private boolean initHdw(Context context) {
        // 加载手写数据
        byte[] hwData = readData(context.getAssets(), "hwdata.bin");
        if (hwData == null) {
            return false;
        }
        WWHandWrite.apkBinding(context);
        if (WWHandWrite.hwInit(hwData, 0) != 0) {
            return false;
        }
        // 初始化联想
        byte[] asseData = readData(context.getAssets(), "asse.bin");
        if (asseData != null) {
            if (WWHandWrite.hwAsseInit(asseData, 0) != 0) {
                return false;// 失败
            }
        }
        mResult1 = new char[256];
        mSBPoint.clear();
        return true;
    }

    /**
     * 识别手势方法
     */
    public boolean recognitionData(List<Short> strokes, HwrRecogResult recogResult) {
        mSBPoint.addAll(strokes);
        int countTemp = mSBPoint.size();
        int i = 0;
        short[] mTracksTemp = new short[countTemp + 2];
        for (short sh : mSBPoint) {
            mTracksTemp[i++] = sh;
        }
        mTracksTemp[i++] = -1;
        mTracksTemp[i] = -1;
        if (sInitState) {
            int code = WWHandWrite.hwRecognize(mTracksTemp, mResult1, 50, 0xFFFF);
            if (code > 0) {
                ArrayList<HwrRecogResultItem> recogItemList = new ArrayList<>();
                for (int index = 0; index < code; index++) {
                    HwrRecogResultItem resultItem = new HwrRecogResultItem();
                    resultItem.setResult(String.valueOf(mResult1[index]));
                    recogItemList.add(resultItem);
                }
                recogResult.setResultItemList(recogItemList);
                return true;
            } else {
                getInstance();
            }
        } else {
            getInstance();
        }
        return false;
    }

    /**
     * 候选词联想方法
     */
    public boolean associateData(String choice, HwrAssociateWordsResult associateResult) {
        if (TextUtils.isEmpty(choice)) {
            return false;
        }
        if (sInitState) {
            int asseCount = WWHandWrite.hwAsseInput(choice.toCharArray(), choice.length());
            if (asseCount > 0) {
                ArrayList<String> associateList = new ArrayList<>();
                for (int i = 0; i < asseCount; i++) {
                    char[] temp = WWHandWrite.hwAsseGet(i);
                    associateList.add(String.valueOf(temp, 0, temp.length));
                }
                associateResult.setResultList(associateList);
                mSBPoint.clear();
                mResult1 = new char[256];
                return true;
            }
        } else {
            getInstance();
        }
        return false;
    }

    /**
     * 释放识别资源
     */
    public void hciHwrRelease() {
        mSBPoint.clear();
        mResult1 = new char[256];
    }
}
